package collection;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;

public class MapUpdate extends JInternalFrame {
	private JLabel lblNewLabel;
	private JTextField findID;
	private JButton btnNewButton;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JTextField irum;
	private JTextField phone;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JTextField status;
	private boolean findFlag;

	String a = "";
	Map<String, MapData> map = new HashMap<String, MapData>();

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// MapUpdate frame = new MapUpdate();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/**
	 * Create the frame.
	 */
	public MapUpdate(HashMap<String, MapData> a) {
		super("Update", true, true, true, false);
		this.map = a;

		setBounds(100, 100, 406, 281);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getFindID());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getIrum());
		getContentPane().add(getPhone());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getBtnNewButton_2());
		getContentPane().add(getStatus());

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("아이디");
			lblNewLabel.setBounds(35, 25, 57, 15);
		}
		return lblNewLabel;
	}

	private JTextField getFindID() {
		if (findID == null) {
			findID = new JTextField();
			findID.setBounds(104, 22, 116, 21);
			findID.setColumns(10);
		}
		return findID;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// if (id.getText().equals("")) {
					// JOptionPane.showMessageDialog(null, "제대로 누르라고");
					// return;
					// }
					// a=id.getText();
					// Set<String> st = map.keySet();
					// Iterator<String> it=st.iterator();
					// while(it.hasNext()){
					// String a = it.next();
					// MapData val = map.get(a);
					//
					// irum.setText("" + val.irum);
					// phone.setText("" + val.phone);

					String find = findID.getText();
					findFlag = false;
					if (find.equals("")) {
						status.setText("검색어할 아이디를 입력하세요.");
					} else {
						MapData d = map.get(find);
						if (d == null) {
							status.setText("찾는 데이터가 없습ㄴ디ㅏ.\n");
							irum.setText("");
							phone.setText("");

						} else {
							findFlag = true;
							irum.setText(d.getIrum());
							phone.setText(d.getPhone());

							status.setText("검색이 완료되었습니다.\n");
						}
					}
				}
			});
			btnNewButton.setBounds(232, 59, 97, 23);
		}
		return btnNewButton;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("성명");
			lblNewLabel_1.setBounds(35, 63, 57, 15);
		}
		return lblNewLabel_1;
	}

	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("연락처");
			lblNewLabel_2.setBounds(35, 106, 57, 15);
		}
		return lblNewLabel_2;
	}

	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setBounds(104, 60, 116, 21);
			irum.setColumns(10);
		}
		return irum;
	}

	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setBounds(104, 103, 116, 21);
			phone.setColumns(10);
		}
		return phone;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (findFlag) {
						int b = JOptionPane.showConfirmDialog(null, "정말수정???");
						if (b == JOptionPane.YES_OPTION) {
							String id = findID.getText();
							String ir = irum.getText();
							String ph = phone.getText();

							MapData d = new MapData(id, ir, ph);
							map.put(id, d);

							status.setText(id + "(이)가 수정 되었씁니다.");
						} else {
							status.setText("수정이 취소 되었습니다");
						}
					} else {
						status.setText("데이터가 검색되지 않았습니다.");
					}
				}
			});
			btnNewButton_1.setBounds(35, 156, 97, 23);
		}
		return btnNewButton_1;
	}

	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("삭제");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (findFlag) {
						int b = JOptionPane.showConfirmDialog(null, "정말삭제");
						if (b == JOptionPane.YES_OPTION) {
							String id = findID.getText();
							map.remove(id);
							status.setText(id + "(이)가 삭제 되었습니다");
						} else {
							status.setText("삭제가 취소 되었습니다");
						}

					} else {
						status.setText("데이터가 검색되지 않았습니다.");
					}

				}
			});
			btnNewButton_2.setBounds(186, 156, 97, 23);
		}
		return btnNewButton_2;
	}

	private JTextField getStatus() {
		if (status == null) {
			status = new JTextField();
			status.setBounds(35, 189, 318, 21);
			status.setColumns(10);
		}
		return status;
	}
}
